<?php
require_once('gbif.occurrence.php');
class Occurrence1 extends Occurrence {

	public $errors;
	public $coreId = 'occurrenceID';
	public $coreIdFlag = false;

	function __construct( $file = NULL ) {
		parent::__construct($file);
		$this->errors = '';
	}

	public function buildLookUp ($occurrenceIndex = 2, $termsIndex = 4 ) {

	# Mapping the terms sheet data into terms array.
		$lookUp = array();
		$first = true;
		if (!$this->fileOpen) $this->openFile();
		$wk = $this->objPHPExcel->setActiveSheetIndex( $termsIndex );
		foreach ($wk->getRowIterator() as $row) {
			$cellIterator = $row->getCellIterator();
			$cellIterator->setIterateOnlyExistingCells(false);
			$j = 0;$ar = array();
			foreach ($cellIterator as $cell) {
				$j++;
				#$value = utf8_decode($cell->getValue());
				$value = $cell->getValue();
				$value = str_replace("\x0B", "", $value);  //remove all vertical tabs in sender string
				$value = trim($value);
				if($first) {
					$lookUp[$value] = $j;
				} else {
					$key = array_search($j,$lookUp);
					if($key == 'label'){
						if($value == '') break;
					}
					$ar[$key] = $value;
				}
			}
			$first = false;
			if($ar['label'] != '') $this->termsArray[$ar['label']] = $ar;
		}

	# Getting the labels in the occurrence sheet.
		$this->labelArray = array();
		if (!$this->fileOpen) $this->openFile();
		$wk = $this->objPHPExcel->setActiveSheetIndex( $occurrenceIndex );
		foreach ($wk->getRowIterator() as $row) {
			$cellIterator = $row->getCellIterator();
			$cellIterator->setIterateOnlyExistingCells(false);
			foreach ($cellIterator as $cell) {
				#$value = utf8_decode($cell->getValue());
				$value = $cell->getValue();
				$value = str_replace("\x0B", "", $value);  //remove all vertical tabs in sender string
				$value = trim($value);
				$this->labelArray[] = $value;
			}
			break;
		}

/*echo '<br> Term : ' . $this->termsArray[$this->labelArray[0]]['term'];
echo '<br> Core Id : ' . $this->coreId;
exit;*/
		if($this->termsArray[$this->labelArray[0]]['term'] == $this->coreId) {
			$this->coreIdFlag = true;
		}
		$this->meta->coreIdFlag = $this->coreIdFlag;
		$this->meta->occurrence2List = $this->labelArray;
		$this->meta->termsList = $this->termsArray;


		$this->arrayDiff = array_diff($this->labelArray, array_keys($this->termsArray));
	}

	public function validateOccurrenceData( $sheetIndex = 2 ) {
		if (!$this->fileOpen) $this->openFile();

		$passed = true;
		$wk = $this->objPHPExcel->setActiveSheetIndex( $sheetIndex );
		$highestRow = $wk->getHighestRow();
		$highestColumn = $wk->getHighestColumn();
		$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);

		$rowIndex = -1;
		# Builds array but removes any blank rows
		foreach ($wk->getRowIterator() as $row) {
			$rowIndex++;
			$blankRow = true;

			$cellIterator = $row->getCellIterator();
 			$cellIterator->setIterateOnlyExistingCells(false);
			$colIndex = -1;
			foreach ($cellIterator as $cell) {
				$colIndex++;


				# checking if the column is accounted for in the terms sheet.
				if(array_key_exists($this->labelArray[$colIndex],$this->termsArray)) {

				$value = $cell->getValue(); // Removed utf8_decode() for unicode compatibility. bko@GBIFS on 12 Dec 11.

				# Check that data do not contain control characters that break the XML transfer format (e.g. vertical tabs).  Remove if included.
				$value = str_replace("\x0B", '', $value);  //remove all vertical tabs in sender string
#ADD MORE CONDITIONS

				# Transform date format for column "dateIdentified," "eventDate," "modified."
				if(in_array($this->termsArray[$this->labelArray[$colIndex]]['term'], array('dateIdentified','eventDate','modified'))) {
					$value = date("Y-m-d", PHPExcel_Shared_Date::ExcelToPHP(trim($value)));
				} else {
					$value = trim($value);
				}

				$this->occurrenceArray[$rowIndex][$colIndex] = $value;

				} # check

				if ($this->occurrenceArray[$rowIndex][$colIndex] != '') {
					$blankRow = false;
				}
			} # column Itretor

			if ($blankRow || $rowIndex == 0) {
				// Clean blank empty rows
				unset($this->occurrenceArray[$rowIndex]);
/*				
			} else {
				print "<pre>";
				print_r( $this->occurrenceArray[$rowIndex] );
*/				
			}
		} # row itretor
		return( $passed );
	}

	public function writeOccurrenceData($path = '') {
		$fp = fopen($path . $this->occurrenceFilename, 'w');
		fputs($fp, "\xEF\xBB\xBF");

		$headerArray = array();
		if(count($this->labelArray)) {
			foreach($this->labelArray as $label) {
				if(array_key_exists($label,$this->termsArray)) {
					$headerArray[] = $this->termsArray[$label]['term'];
				}
			}
		}

		fputcsv_force($fp, $headerArray);
		
		if(is_array($this->occurrenceArray)) {
		foreach($this->occurrenceArray as $rec) {
			fputcsv_force($fp, $rec);
		}
		}
			
		fclose($fp);
	}

}
?>